<?php
/*
 * Created on Sep 12, 2007
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
 //Adds a line in a post to the database keys => values 
 function adminAdd($values, $table) {
 	//loops though each line in a table dump
 //echo print_r($values);
 	foreach ($values as $line) {
 		if(!lineIsEmpty($line)) { //if line is not compleatly empty
 			if(isset($line['remove'])) {
 				if (!deleteScript('id', $line['id'], $table)) {	
 					echo "You Must Remove All Instances of " . $line['Name'] . " before you Delete it. </br>";
			        $colunmName = tableToIDName($table);
			        echo "<a href=\"AdminHome.php?{$colunmName}=". $line['id'] . "\">View All Instances of attempted delete</a> </br>";
 				}
 			} else {
 				unset($line['remove']); //clear remove and update/insert
	 			if(lineExists($line, "id")) { //if it exists update
		 			//echo "Enters update";
		 			updateDatabase($table, $line, array('id' => $line['id']));
				} else { //if not instert
					//echo "Enters insert";
					if(isDuplicate($line, $table)) {
						echo "Can not insert redundent items into database </br> </br>";
					} else {
						insertIntoDatabase($table, $line);
					}
				}
 			}
		}
		
 	}
 }
 
 function addBox($_POST) {
 	$position = $_POST['position'];
 	$containerTypeID=$_POST['containerType'];
 	if($_POST['textTank'] != "") {
		$tank = $_POST['textTank']; 		
 	} else {
 		$tank = $_POST['dropdownTank'];
 	}
 	if($_POST['textTower'] != "") {
 		$tower = $_POST['textTower']; 
 	} else {
 		$tower = $_POST['dropdownTower'];
 	}
 		
 	$insertArray=array("Tank" => $tank,"Tower" => $tower, "Position" => $position, "ContainerTypeID" => $containerTypeID);
 	insertIntoDatabase("Container", $insertArray);
 	$containerID = mysql_insert_id(); //gets is of most revently inserted item;
 	createEmptyBoxes($containerID, $containerTypeID);
 }
 
 //
 function lineIsEmpty($line) {
 	//echo "Line: " . var_dump($line);
 	foreach ($line as $key => $value) {
 		if($value != "" && $key != 'id') {
 			return false;
 		}	
 	}
 	return true;
 }
 /*
  * tells weather the line exists or not $value is the value
  * that should always exist in the line.
  */
 function lineExists($line, $value) {
 	//echo "Line: $line , value: $value line at value = " . $line[$value] . " is set is: " . isset($line[$value]);
 	return ($line[$value]) != "";  //Is set doesn't work in this case so if the string is anthing other than blank'
 }
 
 //Create empty tubes and inserts into a container with containerID
 function createEmptyBoxes($containerID, $containerTypeID) {
 	$result = selectFromDatabase(array("numRows", "numCols"), "ContainerType", array("id" => $containerTypeID));
 	$line = mysql_fetch_array($result, MYSQL_ASSOC);
 	
 	$numRows = $line['numRows'];
 	$numCols = $line['numCols'];
 	$tubes = $numRows * $numCols;
 	//echo "Tubes is $tubes";
 	for($i = 1; $i <= $tubes; $i++) {
 		insertIntoDatabase("FrozenTube", array("CellTypeID" =>  getEmptyCellType(), "ModifiedByID" => getEmptyMod(), "FrozenByID" => getEmptyResearcher(), "ContainerID" => $containerID, "Location" => $i));
 	}
 }
 /*
  * Takes post data from AdminEditBox.php and uses it to add or edit the list of tubes in the database.
  	Needs to process data into for usable by adminAdd
  	where values are followed by ID.
  
  */
  /*
   * Adds formats post data into something admin add can read
   * so it can add specific values to a list of tubes.  Takes an array that contains feilds for everything in a frozenTube as well as FrozenTubeID's 
   * and sets the frozen tube id's to the frozen tube values.
   */
 function addTubes($values) {
 	//Gets out all values from the post data
 	//echo print_r($values);
 	$cellTypeID = $values['CellTypeID'];
 	$frozenByID =$values['FrozenByID'];
 	$modifiedByID = $values['ModifiedByID'];
 	$passageFrozen = $values['PassageFrozen'];
 	$note = $values['Note'];
 	$numberFrozen = $values['NumberFrozen'];
 	$frozenDate = $values['FrozenDate'];
 	unset($values['CellTypeID']);
 	unset($values['FrozenByID']);
 	unset($values['ModifiedByID']);
 	unset($values['PassageFrozen']);
 	unset($values['Note']);
 	unset($values['NumberFrozen']);
 	unset($values['FrozenDate']);
 	unset($values['ContainerID']);
 	unset($values['id']);
 	foreach($values as $k => $v) {
 		$formattedList[$k]['id'] = $k;
 		$formattedList[$k]['CellTypeID'] = $cellTypeID;
 		$formattedList[$k]['FrozenByID'] = $frozenByID;
 		$formattedList[$k]['ModifiedByID'] = $modifiedByID;
 		$formattedList[$k]['PassageFrozen'] = $passageFrozen;
 		$formattedList[$k]['Note'] = $note;
 		$formattedList[$k]['NumberFrozen'] = $numberFrozen;
 		$formattedList[$k]['FrozenDate'] = date2mysql($frozenDate);
 	}
 	adminAdd($formattedList, 'FrozenTube');	
 }
 
 //returns true if more than one admin in users 
 function multipleAdmins() {
	$result = selectFromDatabase(array("Admin"), "Users", array("Admin" => "admin"));
	return (mysql_num_rows($result) > 1);
 }
 
  	/*
 	 * checks to see if the user is not an admin, if no removes
 	 * that user.
 	 * also checks to see if there are any admins in the database
 	 * user can not delete 
 	 * the last admin.
 	 **/
 	 
 function removeUser($array) {
 	$result = selectFromDatabase(array("Admin"), "Users", array("id" => $array['id']));
 	$line = mysql_fetch_array($result);
 	if(multipleAdmins() | $line['Admin'] != "admin") { //if there are is more than one admin, or the user is not an admin ok to deleate.
 		removeFromDatabase("Users", $array);
 		echo "Done";
 	}else {
 		echo "Can Not Delete the Last Administrator.";
 	}
 }
	
/*
 * Takes a $line that contains a list of keys to values
 * if $line contains a name column and if the name in that column already exists in an insert
 * the function returns true.
 */
 
 function isDuplicate($line,$table) {
 	if(isset($line['Name'])) {
 		$result = selectFromDatabase(array('id'), $table, array('Name' => $line['Name']));
 		if(mysql_num_rows($result) > 0) //has a result, redundent entry
 			return true;
 	}
 	return false;
 	//Not set return false
 }
 /* $array is array of keys => values to add to the User table
  * 
  */
 function editUser($array) {
 	//echo print_r($array);
 	if($array[1]['pass'] == $array[0]['pass2']) {
 		unset($array[1]['pass2']);
 		if($array[1]['pass'] == "") {
 			unset($array[1]['pass']);
 		}
 		if(!isset($array[1]['Admin']) & multipleAdmins())
 			$array[1]['Admin'] = 0;
 		adminAdd($array, "Users");
 	} else {
 		echo "Passwords do not match";
 		
 	}
 	



 }
?>
